﻿using System;
using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;

namespace Mysoft.MyCM.Web.Controllers.WebApi
{
	public class BaseExceptionFilterAttribute : ExceptionFilterAttribute
	{
		public override void OnException(HttpActionExecutedContext context)
		{
			// 仅在请求时记录异常
			if (context.Response == null)
			{
				var CalledParams = BaseApiCalledParams.GetParamsFromRequestHeader(context.Request);
				Business.CMLogger.Log(CalledParams.FromApp, CalledParams.FromUser, CalledParams.FromIP, CalledParams.CalledWebApiVerb, CalledParams.CalledWebApiUrl, context.Exception.ToString(), M.MExceptionLevel.Stop);
			}
			context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
			{
				Content = new StringContent(context.Exception.ToString()),
				ReasonPhrase = System.Web.HttpContext.Current.Server.UrlEncode(context.Exception.Message)
			};
		}
	}
}